<?php
class Activity extends CI_Controller {
	
	function Activity (){
		parent::__construct();
		$this->load->model(array('user_model', 'activity_model', 'club_model', 'employee_model'));
	}
	
	function index($report_loaded){
		if($this->session->userdata('ID_BEST_BUDDY')){
			$signUp_date = $this->session->userdata('fecha_inscripcion');
			$next_monday = $signUp_date;
			
			list($year,$month,$day)=explode("-",$signUp_date);
			$jd = GregorianToJD($month, $day, $year); //Transforming the SignUp Date from greagorian format to julian.
			$days = jddayofweek($jd,0); //Getting the day of the week in int that represent the SignUp date, example: Monday = 1.
			
			//If the SignUp date is not Monday, we get the next monday as the first report week.
			if($days != 1){
				$new = mktime(0,0,0, $month,$day,$year) + (8-$days) * 24 * 60 * 60;
				$next_monday=date("Y-m-d",$new);
			}
			
			//Dates must be cycled from the Sign Up date until the present date, and wich report is performed in each week.
			$present_date = strtotime(date('Y-m-d'));
			$current_time = strtotime($next_monday);
			$current_date = $next_monday;
			$reports = array();
			
			while($current_time < $present_date){
				$new_index = count($reports);
				$reports[$new_index]['date_start'] = format_date($current_date);
				$reports[$new_index]['date_end'] = format_date(date('Y-m-d', strtotime('+6 day', $current_time)));
				$reports[$new_index]['report'] = $this->activity_model->get_report_by_date($current_date, $this->session->userdata('id_integrante'));
				if($reports[$new_index]['report'])
					$reports[$new_index]['report'] = $reports[$new_index]['report'][0];
				else
					$reports[$new_index]['report'] = NULL;
				
				$current_time = strtotime('+7 day', $current_time);
				$current_date = date('Y-m-d', $current_time);
			}		
			$data['reports'] = $reports;
			$data['report_loaded'] = $report_loaded;
			$this->load->view('activity/index', $data);
		}
		else{
			$data['message'] = "Acceso denegado";
			$this->load->view('home', $data);
		}
	}
	
	function type_check($str)
	{
		if ($str == '-')
		{
			$this->form_validation->set_message('type_check', 'Debe escoger un tipo de actividad valida');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	function new_report($date_start, $date_end){
		if($this->session->userdata('ID_BEST_BUDDY')){
			$this->form_validation->set_rules('date_start', 'Date_start', 'required');
			$this->form_validation->set_rules('type', 'Type', 'callback_type_check');
			$this->form_validation->set_rules('description', 'Description', 'required');
			$this->form_validation->set_rules('location', 'Location', 'required');
			$this->form_validation->set_rules('mean', 'Mean', 'required');
			$this->form_validation->set_error_delimiters('<div class="error" style="color:#F00; background-color:#FF0;">', '</div>');
			if ($this->form_validation->run() == FALSE){
				$data['types'] = $this->activity_model->get_active_types();
				$data['date_start'] = format_date($date_start);
				$data['date_end'] = format_date($date_end);
				$this->load->view('activity/new_report', $data);
			}
			else{
				$data['ID_INTEGRANTE'] = $this->session->userdata('id_integrante');
				$data['COD_TIPO_ACTIVDAD'] = $_POST["type"];
				$data['medio_actividad'] = $_POST["mean"];
				$data['lugar_actividad'] = $_POST["location"];
				$data['descripcion_actividad'] = $_POST["description"];
				$data['fecha_de_reporte'] = date('Y-m-d');
				$data['fecha_inicio_de_semana'] = change2ymd($_POST["date_start"]);
				
				$this->activity_model->insert_report($data);
				
				$params = array();
				$params['view'] = "emails/new_report";
				$params['data']['date'] = date('d/m/Y');
				$params['data']['user'] = $this->session->userdata('nombre').' '.$this->session->userdata('apellido'); 
				$foundation = $this->employee_model->get_foundation();
				$params['from'] = $foundation['0']['correo_corporativo'];
				$params['name'] = $foundation['0']['nombre_envio_correo'];
				$params['to'] = $foundation['0']['correo_recibo_de_información'];
				$params['subject'] = "Nuevo Reporte";
				send_email($params);
				
				$this->index('report loaded');
			}
		}
		else{
			$data['message'] = "Acceso denegado";
			$this->load->view('home', $data);
		}
	}
	
	function management(){
		if($this->session->userdata('COD_POS_LIDERAZGO') == 100000){
			$data['clubs'] = $this->club_model->all();
			$this->load->view('management/report/index', $data);
		}
		else{
			$data['message'] = "Acceso denegado";
			$this->load->view('home', $data);
		}
	}
	
	function monthly_report($id_club){
		if($this->session->userdata('COD_POS_LIDERAZGO') == 100000){
			$c = $this->club_model->find('id_club', $id_club);
			$c = $c[0];
			
			$current_date = date('Y-m-d');
			list($year,$month,$day)=explode("-",$current_date);
			$month_ago_date = mktime(0,0,0, $month,$day,$year) - 30 * 24 * 60 * 60;
			$month_ago_date = date("Y-m-d",$month_ago_date);
			
			
			$reports = $this->activity_model->get_reports_between_dates_per_club($month_ago_date, $current_date, $c['id_club']);
			for($i=0; $i < count($reports); $i++){
				$reports[$i]['BUDDY'] = $this->user_model->find('id_integrante', $reports[$i]['ID_BEST_BUDDY']);
				$reports[$i]['BUDDY'] = $reports[$i]['BUDDY'][0];
			}
			
			$data['reports'] = $reports;
			$data['club'] = $c;
			$data['dates']['start'] = $current_date;
			$data['dates']['end'] = $month_ago_date;
			
			$this->load->view('management/report/monthly', $data);
			$this->load->helper(array('dompdf', 'file'));
			$html = $this->load->view('management/report/monthly', $data, true);
			//pdf_create($html, 'reporte_club_'.$c['nombre']);
		}
		else{
			$data['message'] = "Acceso denegado";
			$this->load->view('home', $data);
		}
	}
}
?>







